import {AxiosResponse} from "axios";

declare global {
    interface LoginData {
        username: string;
        password: string;
    }

    interface LoginResponse {
        token: string | null;
        refreshToken: string | null;
        scope?: string;
    }

    interface Token {
        token: string;
        username: string
        scope: string;
    }

    interface RequestError {
        response: {
            status: number;
            config: {
                url: string;
            }
            data: {
                errorCode: number;
                message: string;
            }
        }
    }

    interface RequestParams {
        url: string;
        method: Method_Type;
        data: any;
        headers?: any;
        isLoop?: boolean;
        baseURL?: string;
    }

    interface HistoryCmds {
        entityType: "DEVICE",
        entityId: string,
        keys: string | "" | null,
        startTs: number, // 开始时间
        endTs: number, // 结束时间
        interval: number | 1000, // 时间间隔
        limit: number | 100, // 限制条数
        agg: "AVG"
    }

    interface TsSubCmds {
        entityType: "DEVICE",
        entityId: string,
        cmdId: 10,
        keys: string | "" | null,
        scope: "LATEST_TELEMETRY",
    }

    interface AttrSubCmds {
        entityType: "DEVICE",
        entityId: string,
        scope: "CLIENT_SCOPE",
        cmdId: 2,
    }

    //获取设备的数据结构
    interface DeviceParams {
        //获取实时数据
        tsSubCmds: Array<TsSubCmds>,
        //获取历史数据
        historyCmds: Array<HistoryCmds>,
        //获取属性
        attrSubCmds: Array<AttrSubCmds>
    }

    interface ReceiveDevice {
        errorCode: number;
        errorMsg: number;
        subscriptionId: number;
        latestValues: {
            //上次更新时间
            [key: string]: number;
        },
        data: {
            //key:设备名,number:时间, string:值
            [key: string]: Array<Array<[number, string]>>;
        }
    }

    //发送到unity的数据格式
    interface SendUnityData {
        type: MessageType,
        data: any
    }

    type Method_Type = 'get' | 'post' | 'put' | 'delete' | 'patch';
    type PromiseResolve = (value: (PromiseLike<AxiosResponse<any>> | AxiosResponse<any>)) => void
    type PromiseReject = (reason?: any) => void
}

//获取那种类型的数据（实时，历史，属性）
export enum DeviceDataType {
    Ts, History, Attr
}
export enum MessageType {
    setValue = 'setValue',
    setLock = 'setLock',
    run = 'run',
    pause = 'pause',
    stop = 'stop'
}
